"""
支付相关数据库模型
"""
from sqlalchemy import Column, Integer, String, DateTime, Boolean, Enum, ForeignKey, Float, Text
from sqlalchemy.orm import relationship
from datetime import datetime
import enum

# 使用现有的Base
from Models.login.models import Base


class PaymentStatus(str, enum.Enum):
    """支付状态枚举"""
    PENDING = "pending"  # 待支付
    PROCESSING = "processing"  # 处理中
    SUCCESS = "success"  # 支付成功
    FAILED = "failed"  # 支付失败
    CANCELLED = "cancelled"  # 已取消
    CLOSED = "closed"  # 已关闭


class PaymentMethod(str, enum.Enum):
    """支付方式"""
    ALIPAY = "alipay"  # 支付宝
    WECHAT = "wechat"  # 微信支付
    UNIONPAY = "unionpay"  # 银联


class Payment(Base):
    """支付记录表"""
    __tablename__ = "payments"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    payment_no = Column(String(50), unique=True, nullable=False, index=True, comment="支付流水号")
    order_id = Column(Integer, ForeignKey("orders.id"), nullable=False, index=True, comment="订单ID")
    order_no = Column(String(50), nullable=False, index=True, comment="订单号")
    
    # 支付信息
    payment_method = Column(String(20), nullable=False, default=PaymentMethod.ALIPAY.value, comment="支付方式")
    amount = Column(Float, nullable=False, comment="支付金额（元）")
    status = Column(String(20), default=PaymentStatus.PENDING.value, comment="支付状态")
    
    # 第三方支付信息
    third_party_trade_no = Column(String(100), nullable=True, index=True, comment="第三方交易号（如支付宝交易号）")
    third_party_response = Column(Text, nullable=True, comment="第三方响应数据（JSON格式）")
    
    # 回调信息
    notify_data = Column(Text, nullable=True, comment="回调数据（JSON格式）")
    notify_count = Column(Integer, default=0, comment="回调次数")
    last_notify_at = Column(DateTime, nullable=True, comment="最后回调时间")
    
    # 时间字段
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    paid_at = Column(DateTime, nullable=True, comment="支付完成时间")
    expired_at = Column(DateTime, nullable=True, comment="支付过期时间")
    
    # 关系映射
    order = relationship("Order", back_populates="payments")
    refunds = relationship("Refund", back_populates="payment")
    
    def __repr__(self):
        return f"<Payment(id={self.id}, payment_no={self.payment_no}, status={self.status})>"


class RefundStatus(str, enum.Enum):
    """退款状态枚举"""
    PENDING = "pending"  # 待处理
    PROCESSING = "processing"  # 处理中
    SUCCESS = "success"  # 退款成功
    FAILED = "failed"  # 退款失败
    CANCELLED = "cancelled"  # 已取消


class Refund(Base):
    """退款记录表"""
    __tablename__ = "refunds"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    refund_no = Column(String(50), unique=True, nullable=False, index=True, comment="退款流水号")
    payment_id = Column(Integer, ForeignKey("payments.id"), nullable=False, index=True, comment="支付ID")
    order_id = Column(Integer, ForeignKey("orders.id"), nullable=False, index=True, comment="订单ID")
    order_no = Column(String(50), nullable=False, index=True, comment="订单号")
    
    # 退款信息
    refund_amount = Column(Float, nullable=False, comment="退款金额（元）")
    refund_reason = Column(String(500), nullable=True, comment="退款原因")
    status = Column(String(20), default=RefundStatus.PENDING.value, comment="退款状态")
    
    # 第三方退款信息
    third_party_refund_no = Column(String(100), nullable=True, index=True, comment="第三方退款单号")
    third_party_response = Column(Text, nullable=True, comment="第三方响应数据（JSON格式）")
    
    # 回调信息
    notify_data = Column(Text, nullable=True, comment="回调数据（JSON格式）")
    notify_count = Column(Integer, default=0, comment="回调次数")
    last_notify_at = Column(DateTime, nullable=True, comment="最后回调时间")
    
    # 时间字段
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    refunded_at = Column(DateTime, nullable=True, comment="退款完成时间")
    
    # 关系映射
    payment = relationship("Payment", back_populates="refunds")
    order = relationship("Order", back_populates="refunds")
    
    def __repr__(self):
        return f"<Refund(id={self.id}, refund_no={self.refund_no}, status={self.status})>"



